package com.logos.cartoon.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.logos.cartoon.entity.CharacterLiked;
import com.logos.cartoon.req.CharacterLikedReq;
import com.logos.cartoon.result.Result;
import com.logos.cartoon.service.CharacterLikedService;
import com.logos.cartoon.utils.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 角色点赞表(CharacterLiked)表控制层
 *
 * @author rose
 * @since 2025-03-18 17:17:04
 */
@RestController
@RequestMapping("/characterLiked")
@Slf4j
public class CharacterLikedController {
    /**
     * 服务对象
     */
    @Resource
    private CharacterLikedService characterLikedService;

    @Resource
    private LoginUtil loginUtil;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<CharacterLiked> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.characterLikedService.queryById(id));
    }

    /**
     * 新增角色点赞
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody CharacterLikedReq characterLikedReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(characterLikedReq));
            }
            Preconditions.checkNotNull(characterLikedReq.getCharacterId(), "角色id不能为空");
            Preconditions.checkNotNull(characterLikedReq.getStatus(), "点赞状态 1点赞 0不点赞不能为空");
            String loginId = loginUtil.getId();
            characterLikedReq.setLikeUserId(loginId);
            Preconditions.checkNotNull(characterLikedReq.getLikeUserId(), "点赞人不能为空");
            characterLikedService.add(characterLikedReq);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLikedController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增题目点赞表失败");
        }
    }

}

